package cn.newtest.common.dbutils.handlers;

/**
 * @author WangChui(baiyibing@gmail.com)
 * @version 1.0 TODO
 * 
 */
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.RowProcessor;
import org.springframework.jdbc.core.RowCallbackHandler;

// public class RowResult2BeanReader implements ResultReader {
// public class RowResult2BeanReader implements RowCallbackHandler {

public class RowResult2BeanReader implements RowCallbackHandler {

	/**
	 * The Class of beans produced by this handler.
	 */
	private Class type = null;

	/**
	 * The RowProcessor implementation to use when converting rows into beans.
	 */
	private RowProcessor convert = BasicRowProcessor.instance();

	/** List to save results in */
	private final List results;

	/** The counter used to count rows */
	private int rowNum = 0;

	/**
	 * Create a new RowMapperResultReader.
	 * 
	 * @param rowMapper
	 *            the RowMapper which creates an object for each row
	 */
	public RowResult2BeanReader(Class type, RowProcessor convert) {
		this(type, convert, 0);
	}

	/**
	 * Create a new RowMapperResultReader.
	 * 
	 * @param rowMapper
	 *            the RowMapper which creates an object for each row
	 * @param rowsExpected
	 *            the number of expected rows (just used for optimized
	 *            collection handling)
	 */
	public RowResult2BeanReader(Class type, RowProcessor convert,
			int rowsExpected) {
		// Use the more efficient collection if we know how many rows to expect:
		// ArrayList in case of a known row count, LinkedList if unknown
		this.type = type;
		this.results = (rowsExpected > 0) ? (List) new ArrayList(rowsExpected)
				: (List) new LinkedList();
		this.convert = convert;
	}

	/**
	 * Convert the first row of the <code>ResultSet</code> into a bean with
	 * the <code>Class</code> given in the constructor.
	 * 
	 * @return An initialized JavaBean or <code>null</code> if there were no
	 *         rows in the <code>ResultSet</code>.
	 * 
	 * @throws SQLException
	 * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
	 */
	public Object handle(ResultSet rs) throws SQLException {
		return rs.next() ? this.convert.toBean(rs, this.type) : null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.jdbc.core.RowCallbackHandler#processRow(java.sql.ResultSet)
	 *      在mapRow(rs, this.rowNum++)中似乎rowNum没有用到，这里在mapRow方法中没有用，只是自增给外界用的，I
	 *      see
	 */
	public void processRow(ResultSet rs) throws SQLException {
		// this.results.add(this.rowMapper.mapRow(rs, this.rowNum++));
		this.results.add(this.convert.toBean(rs, this.type));
		this.rowNum++;
	}

	public List getResults() {
		return this.results;
	}

	public RowProcessor getConvert() {
		return convert;
	}

	public void setConvert(RowProcessor convert) {
		this.convert = convert;
	}

}
